package main;

public class PruneTree {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode left1 = new TreeNode(0);
        TreeNode right1 = new TreeNode(0);
        TreeNode left2 = new TreeNode(0);
        TreeNode right2 = new TreeNode(0);
        TreeNode left3 = new TreeNode(0);
        TreeNode right3 = new TreeNode(1);
        root.left = left1;
        root.right = right1;
        left1.left = left2;
        left1.right = right2;
        right1.left = left3;
        right1.right = right3;
        System.out.println(new TreeTraverse().inorderTraverse(new PruneTree().pruneTree(root)));
    }

    public TreeNode pruneTree(TreeNode root){
        if (root == null){
            return root;
        }
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.left == null && root.right == null && root.val == 0){
            return null;
        }
        return root;
    }

}
